var msgBox={};
var msgBoxObj=$("#msgBox");
var showBox='';
var showBoxArea="";
msgBox.receiveMsg=function(fromid,from,timestamp,msg){
	if($("#msg-from-"+fromid).length==0){//某个人第一次发送消息
		var msgHtml="<div id='msg-from-"+fromid+"' form='"+from+"'>" +
		"<div class='msg'><div class='msgTime'>"+mill2Time(timestamp,'HH:mm:ss')+"</div>"+"<div class='msgContent'>"+msg+"</div></div>"+
		"</div>";
		msgBoxObj.append($(msgHtml));
		//统计新信息放入showMsgArea中
		var index=msg.indexOf("<img src=");
		if(index>0){//判断消息体中是否有图片
			msg=$(msg).text()+"图片";
		}
		var msgCountsHtml='<div id="msg-counts-from-'+fromid+'" class="msg" form="'+from+'"><div class="msgName">'+from+'</div><div class="msgContent">'+subStr($(msg).html())+'</div><div class="msgCounts">('+1+')</div></div>';
		$("#showMsgArea").find("#msgTitle").after($(msgCountsHtml));
	}else{
		var msgHtml="<div class='msg'><div class='msgTime'>"+mill2Time(timestamp,'HH:mm:ss')+"</div>"+"<div class='msgContent'>"+msg+"</div></div>";
		$("#msg-from-"+fromid).append($(msgHtml));
		var msgCountsObj=$("#showMsgArea").find("#msg-counts-from-"+fromid);
		var index=msg.indexOf("<img src=");
		if(index>0){//判断消息体中是否有图片
			msg=$(msg).text()+"图片";
		}
		if(msgCountsObj.length>0){
			msgCountsObj.find(".msgContent").text(subStr(msg));
			var oldCnts=msgCountsObj.find(".msgCounts").text().replace("(",'').replace(")",'');
			msgCountsObj.find(".msgCounts").text("("+(parseInt(oldCnts)+1)+")");
		}		
	}
	showBox="<div id='showBox'><div class='msgName'>"+from+"</div><div class='msgContent'>"+subStr(msg,12)+"</div><div class='msgCounts'>("+$("#msg-from-"+fromid).find(".msg").length+")</div></div>";
	$("#showBox").remove();
	
	$(showBox).appendTo($('body')).css({
		left:($(window).width()-299)/2,
		top:($(window).height()-64)
	}).mouseover(function(){
		$("#showMsgArea").css({
			left:($(window).width()-299)/2,
			//使showMsgArea稍微盖着点showBox，则可以实现鼠标滑过事件无异常
			top:($(window).height()+2-64-$("#showMsgArea").height())
		}).show().find(".msg").unbind('hover').bind('hover',function(){
			$(this).toggleClass('msgHover');
		},function(){
			$(this).toggleClass('msgHover');
		}).unbind('click').bind('click',function(){
			var friendID=$(this).attr("id").replace("msg-counts-from-","");
			if(!$("#"+friendID).is(":visible")){//如果发送消息的好友没有在列表中展开，则先展开列表再双击
				$("#"+friendID).parent().prev().click();
			}
			$("#"+friendID).dblclick();
		});
	}).mouseleave(function(){
		$("#showMsgArea").hide();
	});
	$("#showMsgArea").hover(function(){
		$(this).show();
	},function(){
		$(this).hide();
	});
	
};


msgBox.removeMessageByFrom=function(fromid){
	if($("#showMsgArea").find(".msg").length<=1){
		//如果是最后一条提醒消息的点击事件，则移除消息提示
		$("#showBox").remove();
	}else{
		$("#showBox").show();
	}
	$("#showMsgArea").find("#msg-counts-from-"+fromid).remove();//该条消息盒子中的消息需要删除
	$("#showMsgArea").hide();
	msgBoxObj.find("#msg-from-"+fromid).remove();
};

/**
 * 截取字符串长度，以...结束
 * @param str
 * @returns
 */
function subStr(str,subLength){
	if(strlen(str)>subLength){
		return str.substring(0,subLength-4)+" ...";
	}
	return str;
}
/**
 * 计算字符串长度
 * @param str
 * @returns {Number}
 */
function strlen(str) {  
    var len = 0;  
    for (var i = 0; i < str.length; i++) {  
        if (str.charCodeAt(i) > 255 || str.charCodeAt(i<0)) len += 2; else len ++;  
    }  
    return len;  
}  

function mill2Time(mills,format){
	var d=new Date(mills);
	var result=format.replace("YYYY",d.getFullYear());
	console.log(result);
	result=result.replace("MM",(d.getMonth()+1)<10?"0"+(d.getMonth()+1):(d.getMonth()+1));
	result=result.replace("DD",d.getDay()<10?"0"+d.getDay():d.getDay());
	result=result.replace("HH",d.getHours()<10?"0"+d.getHours():d.getHours());
	result=result.replace("mm",d.getMinutes()<10?"0"+d.getMinutes():d.getMinutes());
	result=result.replace("ss",d.getSeconds()<10?"0"+d.getSeconds():d.getSeconds());
	return result;
}
